/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 23735
 * Date: 2022-12-16
 * Time: 23:04
 */
public class Solution {
    public int row;
    public int col;
    public void dfs(char[][] grid,int i,int j){
        if(i<0||i>=row||j<0||j>=col||grid[i][j]=='0'){
            return ;
        }
        grid[i][j]='0';
        //上下左右四个方向
        dfs(grid,i-1,j);
        dfs(grid,i+1,j);
        dfs(grid,i,j-1);
        dfs(grid,i,j+1);
    }
    public int numIslands(char[][] grid) {
        this.row=grid.length;
        this.col=grid[0].length;
        int count=0;
        for(int i=0;i<this.row;i++){
            for(int j=0;j<col;j++){
                if(grid[i][j]=='1'){
                    //发现了一个 1 就把与这个 1 相连的所有 1 都置为零,相当于是看作一个整体了
                    count++;
                    dfs(grid,i,j);
                }
            }
        }
        return count;
    }
}
